package com.jly.edu.controller;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jly.edu.common.dto.SysDictItemDto;
import com.jly.edu.common.lang.Const;
import com.jly.edu.common.lang.Result;
import com.jly.edu.common.validated.SaveGroup;
import com.jly.edu.common.validated.UpdateGroup;
import com.jly.edu.entity.SysDictItem;
import com.jly.edu.entity.SysDictType;
import com.jly.edu.service.SysDictItemService;
import com.jly.edu.service.SysDictTypeService;
import com.jly.edu.utils.ConverUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者：纪联友
 * @since 2021-10-26
 */
@RestController
@RequestMapping("/sys-dict-item")
public class SysDictItemController extends BaseController {


    @Autowired
    private SysDictItemService sysDictItemService;
    @Autowired
    private SysDictTypeService sysDictTypeService;



    @PreAuthorize("hasAnyAuthority('sys:item:list')")
    @GetMapping("/list/{id}")
    public Result getSysDictTypeList(@PathVariable(name = "id") Long typeId){
        Assert.notNull(typeId,"typeId不能为空");
        Page page = sysDictItemService.page(getPage(), new QueryWrapper<SysDictItem>().eq("type_id", typeId));

        List<SysDictItemDto> sysDictItems=ConverUtil.convertList(page.getRecords(),SysDictItemDto.class);
        SysDictType dictType = sysDictTypeService.getById(typeId);
        for (SysDictItemDto dictItemDto:sysDictItems) {
            dictItemDto.setType(dictType.getCode());
        }
        page.setRecords(sysDictItems);
        return Result.success(page);
    }

    @PreAuthorize("hasAnyAuthority('sys:item:list')")
    @GetMapping("/info/{id}")
    public Result getSysDictItemById(@PathVariable(name = "id") Long id){
        Assert.notNull(id,"字典项id不能为空!");
        SysDictItem sysDictItem = sysDictItemService.getById(id);
        SysDictItemDto dictItemDto = new SysDictItemDto();
        if(sysDictItem != null){
            SysDictType dictType = sysDictTypeService.getById(sysDictItem.getTypeId());
            dictItemDto.setType(dictType.getCode());
        }
        ConverUtil.convert(sysDictItem,dictItemDto);
        return Result.success(dictItemDto);
    }


    @PreAuthorize("hasAnyAuthority('sys:dict:delete')")
    @DeleteMapping("/delete/{id}")
    public Result deleteDictItem(@PathVariable(name = "id") Long itemId){
        SysDictItem byId = sysDictItemService.getById(itemId);
        if(byId != null && Const.STATUS_ON.equals(byId.getStatus())){
            return Result.fail("字典值在启用中，无法删除！");
        }
        sysDictItemService.removeById(itemId);
        return Result.success(null);
    }


    @PreAuthorize("hasAnyAuthority('sys:item:save')")
    @PostMapping("/save")
    public Result saveDictType(@Validated(SaveGroup.class) @RequestBody SysDictItemDto sysDictItemDto){
        SysDictItem dictItem = sysDictItemService.getOne(new QueryWrapper<SysDictItem>().eq("type_id", sysDictItemDto.getTypeId())
                .eq("name", sysDictItemDto.getName()));
        Assert.isNull(dictItem,"已经存在该字典项！",sysDictItemDto);

        sysDictItemService.save(ConverUtil.convert(sysDictItemDto,SysDictItem.class));
        return Result.success(null);
    }

    @PreAuthorize("hasAnyAuthority('sys:item:update')")
    @PostMapping("/update")
    public Result updateDictType(@Validated(UpdateGroup.class) @RequestBody SysDictItemDto sysDictItemDto){
            SysDictItem sysDictItem = new SysDictItem();
            sysDictItemService.updateById(ConverUtil.convert(sysDictItemDto,sysDictItem));
            return Result.success(null);
    }
}
